package charazay.tool.ihm.overview;

import javax.swing.JComponent;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;

import org.jdesktop.swingx.JXTable;

import java.awt.FlowLayout;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.Vector;

//import charazay.tool.cobjects.PlayerSkills;
import charazay.tool.cobjects.Player;
import charazay.tool.misc.Session;
import charazay.tool.managers.cobjects.PlayerManager;
import charazay.tool.managers.site.PlayerSiteManager;

public class OverviewPanel extends JSplitPane {
	private JXTable table;
	private JScrollPane jsptop;
	private JScrollPane jspbottom;
	private PlayersTableModel table_model;
	private HashMap players;

	private Vector<String> columnNames;
	private Vector<Vector<Object>> data;
	
	private Locale currentLocale = new Locale(Session.language, Session.country);
	private ResourceBundle messages = ResourceBundle.getBundle(
			"charazay.tool.locale.Internationalization", currentLocale);

	/**
	 * Create the panel.
	 */
	public OverviewPanel() {
		// setLayout(new BorderLayout(0, 0));
		setOrientation(JSplitPane.VERTICAL_SPLIT);

		// refreshTablePanel();
		PlayerManager playerManager = new PlayerManager();
		players = playerManager.getPlayers();
		if (players != null && !players.isEmpty()) {
			data = new Vector<Vector<Object>>();
			for (Iterator<Integer> It = players.keySet().iterator(); It
					.hasNext();) {
				Integer player_id = (Integer) It.next();
				Player currentPlayer = (Player) players
						.get(player_id);
				data.add(currentPlayer.asVector());
			}
			buildColumnNames();

			table_model = new PlayersTableModel(data, columnNames);

			table = new JXTable(table_model);
			table.addMouseListener(new java.awt.event.MouseAdapter() {

				@Override
				public void mouseClicked(java.awt.event.MouseEvent e) {
					int row = table.rowAtPoint(e.getPoint());
					int column = table.columnAtPoint(e.getPoint());
					if (row >= 0 && column >= 0) {
						refreshPlayerCardPanel(row);
					}
				}
			});
			table.setColumnControlVisible(false);
			table.getTableHeader().setReorderingAllowed(false);
		}
		
		table.getColumn(17).setMaxWidth(0);
		table.getColumn(17).setMinWidth(0);
		table.getColumn(17).setPreferredWidth(0);
		table.getColumn(17).setResizable(false);
		
		jsptop = new JScrollPane(table);
		setTopComponent(jsptop);

		
		jspbottom = new JScrollPane();
		setBottomComponent(jspbottom);
		setDividerLocation(0.5);
	}

	private void buildColumnNames() {
		columnNames = new Vector<String>();
		columnNames.add(messages.getString("FirstName"));
		columnNames.add(messages.getString("LastName"));

		columnNames.add(messages.getString("Age"));
		columnNames.add(messages.getString("S.I."));
		columnNames.add(messages.getString("Salary"));
		columnNames.add(messages.getString("Height_SHORT"));
		columnNames.add(messages.getString("Weight_SHORT"));

		columnNames.add(messages.getString("Def"));
		columnNames.add(messages.getString("FT"));
		columnNames.add(messages.getString("2p"));
		columnNames.add(messages.getString("3p"));
		columnNames.add(messages.getString("Dri"));
		columnNames.add(messages.getString("Pas"));
		columnNames.add(messages.getString("Spe"));
		columnNames.add(messages.getString("Ftw"));
		columnNames.add(messages.getString("Reb"));
		columnNames.add(messages.getString("Exp"));
		columnNames.add("");

	}

	public void refreshTablePanel() {
		data = new Vector<Vector<Object>>();
		PlayerSiteManager playerManager = new PlayerSiteManager(Session.teamID);
		players = playerManager.request(Session.username, Session.password);
		playerManager.compareAndSave(players);
		for (Iterator<Integer> It = players.keySet().iterator(); It.hasNext();) {
			Integer player_id = (Integer) It.next();
			Player currentPlayer = (Player) players.get(player_id);
			data.add(currentPlayer.asVector());
		}
		columnNames = new Vector<String>();
		buildColumnNames();

		table_model = new PlayersTableModel(data, columnNames);
		table = new JXTable(table_model);
		table.addMouseListener(new java.awt.event.MouseAdapter() {

			@Override
			public void mouseClicked(java.awt.event.MouseEvent e) {
				int row = table.rowAtPoint(e.getPoint());
				int column = table.columnAtPoint(e.getPoint());
				if (row >= 0 && column >= 0) {
					refreshPlayerCardPanel(row);
				}
			}
		});
		table.getTableHeader().setReorderingAllowed(false);
		table.setColumnControlVisible(false);
		table.getColumn(17).setMaxWidth(0);
		table.getColumn(17).setMinWidth(0);
		table.getColumn(17).setPreferredWidth(0);
		table.getColumn(17).setResizable(false);
		jsptop = new JScrollPane(table);
		setTopComponent(jsptop);
	}

	public void refreshPlayerCardPanel(int row) {
		//Vector player = new Vector();
		Integer player_id = (Integer)table.getValueAt(row, 17);
		Player player = (Player)players.get(player_id);
//		for (int i=0; i<table.getColumnCount();i++) {
//			player.add(table.getValueAt(row, i));
//		}
		jspbottom = new JScrollPane(new PlayerCardPanel(player));
		setBottomComponent(jspbottom);
		setDividerLocation(0.5);
	}

}
